package net.sk.china.city.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.sk.china.common.exception.ServiceException;
import net.sk.china.city.utils.PlatformIdHolder;
import net.sk.china.city.service.IArSupermarketCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.sk.china.city.mapper.ArSupermarketCategoryMapper;
import net.sk.china.city.domain.ArSupermarketCategory;

/**
 * 商品分类Service业务层处理
 * 
 * @author gxf
 * @date 2024-04-25
 */
@Service
public class ArSupermarketCategoryServiceImpl implements IArSupermarketCategoryService
{
    @Autowired
    private ArSupermarketCategoryMapper arSupermarketCategoryMapper;

    /**
     * 查询商品分类
     * 
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public ArSupermarketCategory selectArSupermarketCategoryById(Long id)
    {
        return arSupermarketCategoryMapper.selectArSupermarketCategoryById(id);
    }

    /**
     * 查询商品分类列表
     * 
     * @param arSupermarketCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<ArSupermarketCategory> selectArSupermarketCategoryList(ArSupermarketCategory arSupermarketCategory)
    {
        arSupermarketCategory.setPlatformId(PlatformIdHolder.getPlatformId());
        return arSupermarketCategoryMapper.selectArSupermarketCategoryList(arSupermarketCategory);
    }

    /**
     * 新增商品分类
     * 
     * @param arSupermarketCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertArSupermarketCategory(ArSupermarketCategory arSupermarketCategory)
    {
        ArSupermarketCategory category = arSupermarketCategoryMapper.selectOne(new QueryWrapper<ArSupermarketCategory>()
                .eq("category_name", arSupermarketCategory.getCategoryName()));
        if(category != null){
            throw new ServiceException("该类别名已存在");
        }
        arSupermarketCategory.setPlatformId(PlatformIdHolder.getPlatformId());
        return arSupermarketCategoryMapper.insertArSupermarketCategory(arSupermarketCategory);
    }

    /**
     * 修改商品分类
     * 
     * @param arSupermarketCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateArSupermarketCategory(ArSupermarketCategory arSupermarketCategory)
    {
        ArSupermarketCategory category = arSupermarketCategoryMapper.selectOne(new QueryWrapper<ArSupermarketCategory>()
                .eq("id", arSupermarketCategory.getId()));
        if(category != null && arSupermarketCategory.getCategoryName() !=null && !category.getCategoryName().equals(arSupermarketCategory.getCategoryName())){
            ArSupermarketCategory supermarketCategory = arSupermarketCategoryMapper.selectOne(new QueryWrapper<ArSupermarketCategory>()
                    .ne("id", arSupermarketCategory.getId())
                    .eq("category_name",arSupermarketCategory.getCategoryName())
                    .like("platform_id","%" + PlatformIdHolder.getPlatformId() +"%"));
            if(supermarketCategory != null){
                throw new ServiceException("该类别名已存在");
            }
        }
        return arSupermarketCategoryMapper.updateArSupermarketCategory(arSupermarketCategory);
    }

    /**
     * 批量删除商品分类
     * 
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteArSupermarketCategoryByIds(Long[] ids)
    {
        return arSupermarketCategoryMapper.deleteArSupermarketCategoryByIds(ids);
    }

    /**
     * 删除商品分类信息
     * 
     * @param id 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteArSupermarketCategoryById(Long id)
    {
        return arSupermarketCategoryMapper.deleteArSupermarketCategoryById(id);
    }

    @Override
    public List<ArSupermarketCategory> getNameAndId(){
        return arSupermarketCategoryMapper.selectList(new QueryWrapper<ArSupermarketCategory>()
                .like("platform_id","%"+ PlatformIdHolder.getPlatformId() +"%"));
    }
}
